package com.teradata

import utils.{DateUtils, TeradataConnect}

import scala.collection.mutable


object IopSaleWordSplitMain {
  def main(args: Array[String]): Unit = {

    val wordSplit = new IopSaleWordSplit

    val conn = (new TeradataConnect).portalConnect()

    val sql = "SEL SALES_SMS_CONTENT FROM  DBODATA.VGOP_MARKETING_TASKS GROUP BY 1"

    val result = conn.prepareStatement(sql).executeQuery()

    var messages = ""
    while (result.next()) {
      val salesMessage = result.getString(1)
      messages = messages + salesMessage
    }

    val splitResult = wordSplit.segText(messages, 1)
    val wordSum: mutable.HashMap[String, Int] = new scala.collection.mutable.HashMap

    splitResult
      .split("\\s+")
      .foreach(word => {
        if (wordSum.contains(word)) {
          val currentSum = wordSum(word)
          wordSum.update(word, currentSum + 1)
        } else {
          wordSum.update(word, 1)
        }
      })

    //    清空常用语表
    val deleteSql = "del from IOPDATA.YX_CHANG_YONG_YU"
    val preDeleteSql = conn.prepareStatement(deleteSql)
    preDeleteSql.execute()

    val insertMoulde = "ins into IOPDATA.YX_CHANG_YONG_YU values(?,?,?)"
    val preInsertMoulde = conn.prepareStatement(insertMoulde)


    val currentDate = DateUtils.getCurrentDate
    wordSum.toList.foreach(kv => {
      val word = kv._1
      val times = kv._2
      preInsertMoulde.setString(1, currentDate)
      preInsertMoulde.setString(2, word)
      preInsertMoulde.setInt(3, times)
      preInsertMoulde.addBatch()
    })
    preInsertMoulde.executeBatch()


    conn.close()

  }
}
